package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public PageResponseResult findByQuery(ChannelDto dto) {
        //1. 参数校验
        dto.checkParam();

        //2.分页查询
        //2.1  封装分页查询条件
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2 其他查询条件
        LambdaQueryWrapper<AdChannel> queryWapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {//如果名字不为空 使用模糊查询
            queryWapper.like(AdChannel::getName,dto.getName());
        }
        if(dto.getStatus()!=null){//如果状态不为空 使用状态查询
            queryWapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //升序排序
        queryWapper.orderByAsc(AdChannel::getOrd);
        //2.3执行查询操作
        IPage<AdChannel> pageResult = this.page(pageReq, queryWapper);
        //3. 封装成pageReponseResult结果返回
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //1. 参数校验
        String name = channel.getName();
        if(name== null){//名字不能为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名不能为空");
        }
        if(name.length()>10){//长度不能超10
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名长度不能超过10");
        }
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
        }
        channel.setCreatedTime(new Date());
        //保存频道
        this.save(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //1. 校验ID是否为空
        if(channel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道ID不能为空");
        }
        AdChannel oldChannel = this.getById(channel.getId());
        if(oldChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"原有频道为空");
        }
        //名字不为空
        String name = channel.getName();
        if(StringUtils.isNotBlank(name)&& !name.equals(oldChannel.getName())){//频道名称不为空，且更改频道信息
            if(name.length()>10){//长度不能超10
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名长度不能超过10");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        //2.修改频道
        this.updateById(channel);
        //3.响应
        return ResponseResult.okResult();
    }

    /*
    * 删除频道
    * */
    @Override
    public ResponseResult deleteById(Integer id) {
        //如果为空返回异常
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道ID不能为空");
        }
        //查不到返回异常
        AdChannel channel = this.getById(id);
        if(channel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        //状态不对 不允许删除
        if(channel.getStatus()){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"未下架不允许删除");
        }
        //删除
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
